/* conditiontest.c
 *	A program too assure that CreateCondition, DestroyCondition, Wait, Signal, Broadcast syscalls are working correctly
 *	
 *	
 *
 * 	NOTE: for some reason, user programs with global data structures 
 *	sometimes haven't worked in the Nachos environment.  So be careful
 *	out there!  One option is to allocate data structures as 
 * 	automatics within a procedure, but if you do this, you have to
 *	be careful to allocate a big enough stack to hold the automatics!
 */

#include "syscall.h"

int lock1;

int condition1;
int condition2;

void thread1() {
	Write("Thread 1 running.\n", 19, ConsoleOutput);
	Acquire(lock1);
	Wait(condition1, lock1);
	Wait(condition2, lock1);
	Release(lock1);
	Exit(0);
}

void thread2() {
	Write("Thread 2 running.\n", 19, ConsoleOutput);
	Acquire(lock1);
	Wait(condition1, lock1);
	Signal(condition2, lock1);
	Release(lock1);
	Exit(0);
}

void thread3() {
	Write("Thread 3 running.\n", 19, ConsoleOutput);
	Acquire(lock1);
	Broadcast(condition1, lock1);
	Release(lock1);
	Exit(0);
}

int
main()
{
	Write("This is a test of the tests\n", 29, ConsoleOutput);
	
	lock1 = CreateLock("Lock1", 5);
	condition1 = CreateCondition("Condition1", 10);
	condition2 = CreateCondition("Condition2", 10);
	
	if (condition1 == 0)
		Write("Condition1 is index 0\n", 23, ConsoleOutput);
		
	if (condition2 == 1)
		Write("Condition2 is index 1\n", 23, ConsoleOutput);
	
	if (lock1 == 0)
		Write("Lock1 is index 0\n", 18, ConsoleOutput);
		
	Fork(thread1);
	Fork(thread2);
	Fork(thread3);
	
	return 0;
}